package com.healthtop.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.UrlSafeBase64;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;

import java.io.*;

/**
 * * 椿熙堂信息研发部
 * Author: 道长 on 2016/10/13.
 */
@Component
public class QiniuUpload {

    private String accessKey = "UG_E_4YQdE7FDCkfhIkSTztii6hjJ9cWm486NP1J";

    private String secretKey = "HY0Dj-1-bVbJZPUAYT4PopN2-3sGxhj1AWL29eOm";


    public void upload(byte[] bytes,String filename) {
        UploadManager uploadManager = new UploadManager();
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken("avatar");
        try {
            uploadManager.put(bytes, filename, token);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }

    public void upload64Image(String file64,String filename){
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken("avatar");
        try {
            String url = "http://upload.qiniu.com/putb64/-1/key/" + UrlSafeBase64.encodeToString(filename);
            RequestBody rb = RequestBody.create(null, file64);
            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("Content-Type", "application/octet-stream")
                    .addHeader("Authorization", "UpToken " + token)
                    .post(rb).build();
            OkHttpClient client = new OkHttpClient();
            okhttp3.Response response = client.newCall(request).execute();
            response.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void upload64Png(String file64,String filename){
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken("avatar");
        try {
            String url = "http://upload.qiniu.com/putb64/-1/key/" + UrlSafeBase64.encodeToString(filename+".png");
            RequestBody rb = RequestBody.create(null, file64);
            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("Content-Type", "application/octet-stream")
                    .addHeader("Authorization", "UpToken " + token)
                    .post(rb).build();
            OkHttpClient client = new OkHttpClient();
            okhttp3.Response response = client.newCall(request).execute();
            response.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public InputStream download(String url){
        try{
            byte[] bytes = Jsoup.connect(url).timeout(5000).maxBodySize(10000000).ignoreContentType(true).execute().bodyAsBytes();
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            return inputStream;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getMobileQiniuToken(){
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken("avatar");
        return token;
    }

    public static void main(String[] args) throws IOException {
        QiniuUpload qiniuUpload = new QiniuUpload();
        String path = "/home/skie/IdeaProjects/olds/";
        byte[] buffer = null;
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            for (File file2 : files) {
                FileInputStream in = new FileInputStream(file2);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] b = new byte[1024];
                int n;
                while ((n = in.read(b)) != -1)
                {
                    bos.write(b, 0, n);
                }
                in.close();
                bos.close();
                buffer = bos.toByteArray();
                qiniuUpload.upload(buffer,file2.getName());
                System.out.println(file2.getName()+ "上传完成！");
            }
        }
        System.out.println("上传完成");
    }


}

